package com.lxj.role.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxj.role.mapper.RoleMapper;
import com.lxj.role.service.RoleService;
import com.lxj.utils.common.CommonResult;
import com.lxj.utils.entity.Permission;
import com.lxj.utils.entity.Role;
import com.lxj.utils.entity.RolePermission;
import com.lxj.utils.util.BuildTree;
import com.lxj.utils.vo.RolePermissionRelation;
import com.lxj.utils.vo.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色服务层
 *
 * @author 李学江
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    /**
     * 查询所有
     */
    public CommonResult findAll(Role role) {
        PageHelper.startPage(role.getPageNum(), role.getPageSize());
        PageInfo<Role> pageInfo = new PageInfo<>(this.roleMapper.findAll(role));
        HashMap<String, Object> map = new HashMap<>();
        map.put("number", pageInfo.getTotal());
        map.put("list", pageInfo.getList());
        return CommonResult.success(map);
    }

    @Override
    public CommonResult delete(Integer[] id) {
        int count = roleMapper.delete(id);
        if (count != 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed("错误的请求");
    }

    @Override
    public CommonResult edit(Role role) {
        int result = roleMapper.edit(role);
        if (result != 0) {
            return CommonResult.success(result);
        }
        return CommonResult.failed("错误的请求");
    }

    @Override
    public CommonResult add(Role role) {
        int result = roleMapper.add(role);
        if (result == 1) {
            return CommonResult.success(result);
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult addPs(RolePermission rolePermission) {
        int result = roleMapper.addPs(rolePermission);
        if (result == 1) {
            return CommonResult.success(result);
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult findById(Integer id) {
        Role role = roleMapper.findById(id);
        Assert.notNull(role, "没有角色信息，请联系管理员");
        return CommonResult.success(role);
    }

    /**
     * 根据角色id查询当前角色所拥有的权限
     * @param id
     * @return
     */
    @Override
    public CommonResult findByRoleId(Integer id) {
        List<RolePermissionRelation> list = roleMapper.findByRoleId(id);
        return CommonResult.success(list);
    }

    /**
     * 添加权限
     * @param ids 参数是权限id的一个集合
     * @return
     */
    @Override
    public CommonResult addPermission(Integer[] ids,Integer id) {
        int result = roleMapper.addPermission(ids,id);
        Assert.notNull(result,"添加失败");
        return CommonResult.success(result);
    }

    /**
     * 剔除权限
     * @param ids 权限id的集合
     * @return
     */
    @Override
    public CommonResult deletePermission(Integer[] ids,Integer id) {
        int result = roleMapper.deletePermission(ids,id);
        Assert.notNull(result,"剔除失败");
        return CommonResult.success(result);
    }

    @Override
    public CommonResult searchById(Integer id) {
        Role role = roleMapper.searchById(id);
        //定义一个空的类型为Tree的集合
        List<Tree<Permission>> trees = new ArrayList<>();
        //查询出拥有的所有权限信息
        List<Permission> list = roleMapper.findPermission(id);
        for (Permission all:list){
            Tree<Permission> tree = new Tree<>();
            tree.setId(all.getId());
            tree.setPid(all.getPid());
            tree.setTitle(all.getName());
            Map<String,Object> map = new HashMap<>(16);
            Integer perId = all.getPid();
            trees.add(tree);
        }
        Tree<Permission> t = BuildTree.build(trees);
     Map<String,Object> map = new HashMap<>();
     map.put("role",role);
     map.put("tree",t);
     return CommonResult.success(map);
    }

    @Override
    public CommonResult searchAll() {
        List<Role> list = roleMapper.searchAll();
        return CommonResult.success(list);
    }
}
